﻿<%@ WebHandler Language="C#" Class="WishHandler" %>

using System;
using System.Web;
using Util;
using Manager;

/// <summary>
/// 读取纸条数据
/// 注意：jQuery1.4以后严格了json格式，日期以 new Date(..)的形式
/// 返回会出现parsererror错误，整个项目现改为用jQuery1.3.2库
/// </summary>
public class WishHandler : BaseAjaxHandler
{
    /// <summary>
    /// 映射方法
    /// </summary>
    protected override void Mapping()
    {
        //OprationMap["get"] = "GetWish";                  // 获得某个纸条
        OprationMap["gets"] = "GetWishes";               // 获得多个纸条
        //OprationMap["count"] = "GetWishesCount";         // 获得纸条总数
        OprationMap["add"] = "AddWish";                  // 添加纸条
        //OprationMap["listIconCls"] = "ListIconClass";    // 列出纸条图片分类
        OprationMap["getIcons"] = "GetIcons";            // 获得纸条图片
        OprationMap["initAdd"] = "InitAddFormData";      // 添加窗体初始化时的数据
    }

    /// <summary>
    /// 获得指定主题的指定页纸条,含纸条总数
    /// </summary>
    public void GetWishes()
    {
        int sid = int.Parse(Context.Request["sid"]);
        string sPage = Context.Request["page"];
        string sPageSize = Context.Request["size"];
        int page = 1;
        int size = Constants.DEFAULT_PAGE_SIZE;

        if (sPage != null)
            page = int.Parse(sPage);
        if (sPageSize != null)
        {
            size = int.Parse(sPageSize);
        }
        

        int start = (page - 1) * size;
        int limit = size;
        
        
        WishManager m = new WishManager();
        Object wishes = m.GetWishes(sid, start, limit);
        
        int count = m.GetWishesCount(sid);
        
        var data = new {
            page = page,
            count = count,
            size = limit,
            wishes = wishes
        };
        
        WriteTemplateResponse(true, JsonHelper.Serialize(data));
    }

    /*
    /// <summary>
    /// 某主题获得纸条总数
    /// </summary>
    public void GetWishesCount()
    {
        int sid = int.Parse(Context.Request["sid"]);

        WishManager m = new WishManager();
        int count = m.GetWishesCount(sid);

        WriteTemplateResponse(true, count.ToString());
    }*/
    /// <summary>
    /// 添加纸条
    /// </summary>
    public void AddWish()
    {
        string validateCode = Context.Request["validateCode"];

        int subjectId = int.Parse(Context.Request["subject"]);

        SubjectManager sm = new SubjectManager();
        Subject subject = sm.GetSubject(subjectId);


        if (subject.voteLimit == Constants.ADD_WISH_HAS_PASSWORD)    // 需要密码才能添加
        {
            if (!validateCode.Equals(subject.voteCode))
            {
                Message = "密码错误";
                return;
            }
        }
        else
        {
            // 验证码是否正确
            if (!validateCode.ToUpper().Equals(Context.Session[Constants.SESS_VALIDATE_CODE_KEY].ToString().ToUpper()))
            {
                //WriteTemplateResponse(false, "验证码错误");
                Message = "验证码错误";
                return;
            }
        }
        
        string sender = Context.Request["sender"];
        string words = Context.Request["words"];
        string icon = Context.Request["icon"];
        string shape = Context.Request["shape"];
        string color = Context.Request["color"];
        
        if (icon == null)
            icon = "icon0.gif";
        if (shape == null)
            shape = "default";
        if (color == null)
            color = "green";
        
        try
        {
            WishManager m = new WishManager();
            Wish w = new Wish
            {
                subjectId = subjectId,
                sender = sender,
                words = words,
                img = icon,
                color = color,
                shape = shape,
                createTime = DateTime.Now,
                senderIp = Context.Request.UserHostName
            };

            m.Add(w);
        }
        catch (Exception e)
        {
            LogUtil.ErrorException(e);
        }
        WriteTemplateResponse(true, JsonHelper.Serialize("添加成功"));
    }

    /*
    /// <summary>
    /// 返回字条图片分类JSON数据
    /// </summary>
    public void ListIconClass()
    {
        WishManager m = new WishManager();

        string data = JsonHelper.Serialize(m.GetWishImgClasses());

        WriteTemplateResponse(true, data);
    }*/

    /// <summary>
    /// 返回某分类的图片
    /// </summary>
    public void GetIcons()
    {
        int clsId = int.Parse(Context.Request["clsId"]);

        WishManager m = new WishManager();
        string data = JsonHelper.Serialize(m.GetWishImgs(clsId));

        WriteTemplateResponse(true, data);
    }

    /// <summary>
    /// 添加纸条窗体初始化时所需数据
    /// </summary>
    public void InitAddFormData()
    {
        WishManager m = new WishManager();
        
        var data = new
        {
            shape = m.GetWishShapes(),
            color = m.GetWishColors("default"),
            iconCls = m.GetWishImgClasses(),
            icon = m.GetWishImgs(1)
        };

        WriteTemplateResponse(true, JsonHelper.Serialize(data));
    }
    
}